package com.haier.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

public class HttpUtils {
	public static String get(String url, Map<String, String> headMap) throws Exception {
		HttpGet httpGet = new HttpGet(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpGet.addHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static InputStream down(String url, Map<String, String> headMap) throws Exception {
		url = url.replaceAll("\\\\", "/");
		HttpGet httpGet = new HttpGet(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpGet.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		InputStream input = entity.getContent();
		return input;
	}

	public static String post(String url, Map<String, String> headMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static String post(String url, List<NameValuePair> params, Map<String, String> headMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		// 添加参数
		httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static String post(String url, String entityString, Map<String, String> headMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		StringEntity stringEntity = new StringEntity(entityString, Charset.forName("UTF-8"));
		stringEntity.setContentType("application/json");
		stringEntity.setContentEncoding("utf-8");
		httpPost.setEntity(stringEntity);
		httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static String upload(String url, MultipartFile file, Map<String, String> headMap) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		String fileName = file.getOriginalFilename();
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
		httpPost.setEntity(builder.build());
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpPost.addHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	public static String delete(String url, Map<String, String> headMap) throws Exception {
		HttpDelete httpDelete = new HttpDelete(url);
		if (headMap != null) {
			for (String key : headMap.keySet()) {
				httpDelete.setHeader(key, headMap.get(key));
			}
		}
		HttpClient client = HttpClientBuilder.create().build();
		HttpResponse response = client.execute(httpDelete);
		HttpEntity entity = response.getEntity();
		String resBody = EntityUtils.toString(entity, "UTF-8");
		return resBody;
	}

	/**
	 * 将流 保存为数据数组
	 * 
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		// 创建一个Buffer字符串
		byte[] buffer = new byte[1024];
		// 每次读取的字符串长度，如果为-1，代表全部读取完毕
		int len = 0;
		// 使用一个输入流从buffer里把数据读取出来
		while ((len = inStream.read(buffer)) != -1) {
			// 用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
			outStream.write(buffer, 0, len);
		}
		// 关闭输入流
		inStream.close();
		// 把outStream里的数据写入内存
		return outStream.toByteArray();
	}

}
